package com.animatedpicture.panel;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.animatedpicture.object.Audience;
import com.animatedpicture.object.Bass;
import com.animatedpicture.object.Drum;
import com.animatedpicture.object.Guitar;
import com.animatedpicture.object.IMovingObject;
import com.animatedpicture.object.Keyboard;
import com.animatedpicture.object.Light;
import com.animatedpicture.object.Vocalist;

public class ButtonPanel extends JPanel {

	private ArrayList<JButton> btnList = new ArrayList<>();
	private String[] itemList = {"Drum", "Keyboard", "Guitar", "Bass", "Vocal", "Audience", "Light", "Animate", "Clear"};
	private String[] imgList = {"Default", "Yap Che Een", "Kim Ik Soo", "Wong Kok Sheng", "Shin Yong Tae", "Kim Seock Yoon"};
	
	// Interface Object
	private IMovingObject movingObj;
	
	private DrawPanel drawPnl = new DrawPanel();
	
	public ButtonPanel(JFrame j)
	{
		// Create User Interface
		setPreferredSize(new Dimension((int)(j.getWidth()*0.15), j.getHeight()));
		setLayout(new GridLayout(itemList.length, 1));
		
		// Add Buttons
		for (int i=0; i<itemList.length; i++)
		{
			String itemName = itemList[i];
			JButton btn = new JButton(itemName);
			btn.setHorizontalTextPosition(SwingConstants.LEFT);
			
			// Add Action Listener
			btn.addActionListener(new ButtonClickListener());
			btnList.add(btn);
			
			add(btn);
		}
		
		// After application launched, disable animation and clear button at first.
		btnList.get(btnList.size()-1).setEnabled(false);
		btnList.get(btnList.size()-2).setEnabled(false);
		
		// Initialize instance variable
		movingObj = null;
	}
	
	// show Dialog Method
	private String showDialog()
	{
		String input = (String)JOptionPane.showInputDialog(null,
				   "Choose Image",             // Message
				   "Choose...",                // Title
				   JOptionPane.QUESTION_MESSAGE,
				   null,
				   imgList,
				   imgList[0]);
		
		return input;
	}

	// ButtonClickListnesr
	class ButtonClickListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) {
			
			JButton clickedBtn = (JButton)e.getSource();

			switch (clickedBtn.getText()) {
			case "Drum":
				movingObj = new Drum(showDialog());
				break;
			case "Keyboard":
				movingObj = new Keyboard(showDialog());
				break;
			case "Guitar":
				movingObj = new Guitar(showDialog());
				break;
			case "Bass":
				movingObj = new Bass(showDialog());
				break;
			case "Vocal":
				movingObj = new Vocalist(showDialog());
				break;
			case "Audience":
				movingObj = new Audience(showDialog());
				break;
			case "Light":
				movingObj = new Light();
				break;
			case "Animate":
				// Change Button's name (to "Stop")
				btnList.get(btnList.size()-2).setText("Stop");
				drawPnl.animateItem();
				return;
			case "Stop":
				// Change Button's name (to "Animate")
				btnList.get(btnList.size()-2).setText("Animate");
				drawPnl.stopAnimate();
				return;
			case "Clear":
				btnList.get(btnList.size()-2).setText("Animate");
				drawPnl.stopAnimate();
				drawPnl.clear();
				btnList.get(btnList.size()-1).setEnabled(false);
				btnList.get(btnList.size()-2).setEnabled(false);
				return;
			default:
				break;
			}
			
			// After object is created, add to DrawPanel
			drawPnl.addItem(movingObj);
			
			// Enable animate and clear button
			btnList.get(btnList.size()-1).setEnabled(true);
			btnList.get(btnList.size()-2).setEnabled(true);
		}
		
	}
}
